﻿using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;
using Nemerle.Compiler;

using System;
using System.Collections.Generic;
using System.Linq;

using N2 = Nemerle2.Compiler;
using PT = Nemerle.Compiler.Parsetree;

namespace Nemerle2.Parser
{
  public partial class AstConverter
  {
    ConvertNamespace(env : GlobalEnv, ns : N2.PNamespace) : void
    {
      match(ns)
      {
        | Content(name, usings, _, namespaces, typeDeclations) =>
          def env = usings.Aggregate(env, ConvertUsing);
          def env = if(name.Count > 0) env.EnterIntoNamespace(ToStringList(name)) else env;
          foreach(decl in typeDeclations)
            ConvertTypeDeclaration(env, decl);
          foreach(ns in namespaces)
            ConvertNamespace(env, ns);

        | Error(msg) as loc => Echo(loc, msg);
      }
    }

    ConvertUsing(env : GlobalEnv, u : N2.PUsing) : GlobalEnv
    {
      def loc = NLocation(u);
      match(u)
      {
        | Open(path)        => env.AddOpenNamespace(ToStringList(path), loc)
        | Alias(name, path) => env.AddNamespaceAlias(name.Text, ToStringList(path), loc)
        | Error(msg)        => Echo(loc, msg); env
      }
    }
  }
}
